import Vue from 'vue'
import Vuex from 'vuex'
import { getSession, setSession } from '@/utils/session'
import {
  sysColumnControlQuery,
  // sysColumnControlDelete,
  sysColumnControlAdd,
  sysColumnControlUpdate,
  sysColumnControlOpenApiQuery,
  sysColumnControlAddBatch,
  sysColumnControlDeleteByType,
} from '@/api/home/sysColumnControl'

Vue.use(Vuex)

const state = {}

const getters = {}

const actions = {
  async querySysColumnControl({ commit }, listQuery) {
    let res = await sysColumnControlQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async queryOpenApiSysColumnControl({ commit }, listQuery) {
    let res = await sysColumnControlOpenApiQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  // async delSysColumnControl({ commit }, row) {
  //   let res = await sysColumnControlDelete(row.id)
  //   return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  // },
  async addSysColumnControl({ commit }, data) {
    let res = await sysColumnControlAdd(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async updateSysColumnControl({ commit }, data) {
    let res = await sysColumnControlUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async getHomeConfig({ dispatch }) {
    let homeConfig = null
    let obj = { page: 1, limit: 999 }
    let res = await dispatch('queryOpenApiSysColumnControl', obj)
    homeConfig = res.data
    setSession('homeConfig', JSON.stringify(homeConfig))
    return homeConfig
  },
  async addBatchSysColumnControl({ commit }, data) {
    let res = await sysColumnControlAddBatch(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async deleteByTypeSysColumnControl({ commit }, type) {
    let res = await sysColumnControlDeleteByType(type)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
}

const mutations = {}

export default {
  state,
  getters,
  actions,
  mutations,
}
